<?php

namespace admin\controllers;

use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use common\models\Book;
use common\models\BookChapter;
use admin\models\BookChapterSearch;


/**
 * BookChapterController implements the CRUD actions for BookChapter model.
 */
class BookChapterController extends Controller
{

    public function beforeAction($action)
    {
        if (!parent::beforeAction($action)) {
            return false;
        }

        if (!($bookId = Yii::$app->request->get('book_id')) || !($book = Book::findOne($bookId))) {
            throw new NotFoundHttpException;
        }

        Yii::$app->set('book', $book);
        return true;
    }

    /**
     * Lists all BookChapter models.
     * @return mixed
     */
    public function actionIndex($book_id)
    {
        $searchModel = new BookChapterSearch();
        $searchModel->book_id = $book_id;
        
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Creates a new BookChapter model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate($book_id)
    {
        $model = new BookChapter();
        $model->book_id = $book_id;

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index', 'book_id' => $book_id]);
        }

        //查询作品最大display_order
        $lastChapter = BookChapter::find()->andWhere(['book_id' => $book_id])->orderBy(['display_order' => SORT_DESC])->one();

        $display_order = 0;
        if ($lastChapter) {
            $display_order = $lastChapter->display_order + 1;
        }

        return $this->render('create', [
            'model' => $model,
            'display_order' => $display_order,
        ]);
    }


    /**
     * Updates an existing BookChapter model.
     * @param int $book_id 作品id
     * @param integer $id 章节id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($book_id, $id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index', 'book_id' => $book_id]);
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }


    /**
     * Deletes an existing BookChapter model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($book_id, $id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index', 'book_id' => $book_id]);
    }

    /**
     * Finds the BookChapter model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return BookChapter the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = BookChapter::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}
